<?php

namespace app\common\model;

use think\Model;
use traits\model\SoftDelete;

class Banner extends Model
{
    //软删除
    use SoftDelete;

    //banner图添加
    public function banner_add($data)
    {
        if (!$data){
            return '图片不能为空!';
        }
        $info = $data -> move(ROOT_PATH.'public'.DS.'uploads');
        if ($info){
            $date['img'] = '/uploads/'.$info -> getSaveName();
        } else {
            return $data -> getError();
        }
        //dump($date['img']);
        $result = $this -> save($date);
        if ($result){
            return 1;
        } else {
            return '图片保存失败！';
        }
    }

    //banner图状态控制
    public function banner_status($data)
    {
        $bannerInfo = $this -> find($data['id']);
        $bannerInfo -> status = $data['status']?0:1;
        $result = $bannerInfo -> save();
        if ($result){
            return 1;
        } else {
            return '操作失败！';
        }
    }

    //banner图编辑
    public function banner_edit($data)
    {
        $date = $this -> find($data['id']);
        if (!$data['file']){
            return '图片不能为空!';
        }
        $info = $data['file'] -> move(ROOT_PATH.'public'.DS.'uploads');
        if ($info){
            $banner = '/uploads/'.$info -> getSaveName();
        } else {
            return $data -> getError();
        }
        $date -> img = $banner;
        $result = $date -> save();
        if ($result){
            return 1;
        } else {
            return '编辑失败~！';
        }
    }


}
